/*
 * Copyright (C) 2018 wxyz <hyhjwzx@126.com>
 * This program can be distributed under the terms of the GNU GPL Version 2.
 * See the file LICENSE.
 *
 */

package ren.wxyz.study.euler.hp01;

import ren.wxyz.study.euler.base.IProblem;
import ren.wxyz.study.euler.util.ConsoleHelper;

/**
 * 第 4 题
 *
 * @auther wxyz 2018-02-28_21:03
 * @since 1.0
 */
public class P004 implements IProblem {
    @Override
    public void main(String[] args) throws Throwable {
        long minNumber = Long.parseLong(args[0]);
        long maxNumber = Long.parseLong(args[1]);

        long a = 0;
        long b = 0;

        long maxProduct = 0;
        for (long i = minNumber; i <= maxNumber; i++) {
            for (long j = minNumber; j <= maxNumber; j++) {
                long tmpNumber = i * j;
                if (isPalindromic(tmpNumber)) {
                    a = i;
                    b = j;

                    if (tmpNumber > maxProduct) {
                        maxProduct = tmpNumber;
                    }
                }
            }
        }

        ConsoleHelper.printf("%1$d = %2$d * %3$d \r\n", maxProduct, a, b);
    }

    /**
     * 判断一个数是回文数
     *
     * @param num 给定的数
     * @return 是回文数返回true
     */
    private boolean isPalindromic(long num) {
        String strNum = String.valueOf(num);
        int strLen = strNum.length();
        for (int i = 0; i < strLen / 2; i++) {
            if (strNum.charAt(i) != strNum.charAt(strLen - i - 1)) {
                return false;
            }
        }

        return true;
    }
}
